let {
    expense
} = require("./expense_schema.js");
module.exports = {
    expenseAdd: function (req, res, err) {
        console.log(req.body, "expense");
        let userinfo = req.body;
        let userQ = userinfo.user;
        // 用户信息
        let yearQ = userinfo.item[0].yearDate;
        let monQ = userinfo.item[0].yearDetails[0].monDate;
        let dayQ = userinfo.item[0].yearDetails[0].monDetails[0].dayDate;
        let moneyQ = Number(userinfo.item[0].yearDetails[0].monDetails[0].dayDetails[0].money);
        let yearItem = userinfo.item[0];
        let monItem = userinfo.item[0].yearDetails[0];
        let dayItem = userinfo.item[0].yearDetails[0].monDetails[0]
        expense.find({
            user: userQ
        }).then((d0) => {
            if (d0.length > 0) {
                let userS = d0[0];
                console.log("用户信息存在");
                let itemS = userS.item;
                // 逻辑块
                // 判断年份是否存在
                let isYear = null;
                let index = null;
                itemS.forEach((T, I) => {
                    if (T.yearDate == yearQ) {
                        isYear = true;
                        index = I;
                    }
                })
                if (isYear) {
                    console.log("年份存在", index);
                    let isMon = null;
                    let index1 = null;
                    // 判断月份是否存在
                    itemS[index].yearDetails.forEach((T1, I1) => {
                        if (T1.monDate == monQ) {
                            isMon = true;
                            index1 = I1;
                        }
                    })
                    if (isMon) {
                        console.log("月份存在", index1);
                        let isDay = null;
                        let index2 = null;
                        //判断天数是否存在
                        itemS[index].yearDetails[index1].monDetails.forEach((T2, I2) => {
                            if (T2.dayDate == dayQ) {
                                isDay = true;
                                index2 = I2;
                            }
                        })
                        if (isDay) {
                            console.log("天数存在", index2);
                            userS.item[index].yearDetails[index1].monDetails[index2].dayTotal += moneyQ;
                            userS.item[index].yearDetails[index1].monDetails[index2].dayDetails.push(dayItem.dayDetails[0]);
                        } else {
                            console.log("天数不存在", index2);
                            dayItem.dayTotal = moneyQ;
                            userS.item[index].yearDetails[index1].monDetails.push(dayItem);
                        }
                        userS.item[index].yearDetails[index1].monTotal += moneyQ;

                    } else {
                        // 年份不存在是创建新月份数据
                        console.log("月份不存在", index1);
                        monItem.monTotal = moneyQ;
                        monItem.monDetails[0].dayTotal = moneyQ;
                        userS.item[index].yearDetails.push(monItem);
                    }
                    userS.item[index].yearTotal += moneyQ;
                } else {
                    // 年份不存在是创建新年份数据
                    yearItem.yearTotal = moneyQ;
                    yearItem.yearDetails[0].monTotal = moneyQ;
                    yearItem.yearDetails[0].monDetails[0].dayTotal = moneyQ;
                    userS.item.push(yearItem);
                }
                userS.total += moneyQ;
                expense.updateOne({
                    user: userQ
                }, {
                    $set: userS
                }).then((d9) => {
                    console.log(d9);
                })
            } else {
                userinfo.total = moneyQ;
                userinfo.item[0].yearTotal = moneyQ;
                userinfo.item[0].yearDetails[0].monTotal = moneyQ;
                userinfo.item[0].yearDetails[0].monDetails[0].dayTotal = moneyQ;
                expense.create(userinfo);
                console.log("创建用户信息");
            }
        })
        res.send(true)
    }
}